﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using Gift.Model.ENUM;

namespace WebSite.admin.order
{
    public partial class OrderCancel : BaseAdmin
    {

        // Methods
        private void Bind()
        {
            if ((base.Request.QueryString["oid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    string sOid = base.Request.QueryString["oid"].ToString();

                    string sSQL = " update DB_order set states = 5 where orderid=" + sOid + "; "
                                    + " update DB_Card set states = 1 where cardid in (select OA.cardid from tbOrderAccount OA join  DB_Order O on OA.OrderCode = O.OrderCode  where O.Orderid=" + sOid + ");";
                                    //+ " update DB_Member set [money] = [money] +  (select sum(sumprice) from DB_Car where Orderid=" + sOid + " )  where memberid in  (select O.memberid from  DB_Order O  where O.Orderid=" + sOid + ");";
                    SQLAccess.DoSql(sSQL);

                }
                base.Response.Write("<script language='javascript'>alert('取消成功!');window.location.href='OrderCancel.aspx';</script>");
                base.Response.End();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
               
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {

                        DataTable table = SQLAccess.GreatDs("select payname from DB_Pay where payid in (" + e.Row.Cells[3].Text + ") group by payname").Tables[0];
                        e.Row.Cells[3].Text = "";
                        foreach (DataRow dr in table.Rows)
                        {
                            e.Row.Cells[3].Text = e.Row.Cells[3].Text != "" ? e.Row.Cells[3].Text + ";" + dr[0].ToString() : dr[0].ToString();
                        }

                        e.Row.Cells[4].Text = info.info1("", "shophuobi") + string.Format("{0:f}", Convert.ToSingle(e.Row.Cells[4].Text)) + " 元";
                        if (e.Row.Cells[7].Text.ToString() == "0")
                        {
                            e.Row.Cells[7].Text = "新订单";
                        }
                        else if (e.Row.Cells[7].Text.ToString() == "1")
                        {
                            e.Row.Cells[7].Text = "备货中";
                        }
                        else  if (e.Row.Cells[7].Text.ToString() == "2")
                        {
                            e.Row.Cells[7].Text = "已发货";
                        }
                        else  if (e.Row.Cells[7].Text.ToString() == "3")
                        {
                            e.Row.Cells[7].Text = "已完成";
                        }
                        else if (e.Row.Cells[7].Text.ToString() == "5")
                        {
                            e.Row.Cells[7].Text = "已取消";
                        }
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = GetSql();
            this.GridViewBind(sqlsort);
        }

        private string GetSql()
        {
            string sql = "select *,(select isnull(sum(sumprice),0) from DB_Car where orderid=DB_Order.orderid)  as sumprice from DB_order where (states=0 or states=1) and languages=" + info.languages("") + " ";

            if (this.ordercode.Value.ToString() != "")
            {
                sql = sql + " and ordercode like '%" + this.ordercode.Value.ToString().Trim() + "%'";
            }
         
            sql = sql + " order by ordertime desc";
            return sql;
        }

        private void GridViewBind(string Sqlsort)
        {
            string sql = GetSql();
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }





        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    if ((base.Request.Form["flag"].ToString() == "1") || (base.Request.Form["flag"].ToString() == "2"))
                    {
                        DataTable table = SQLAccess.GreatDs("select memberid,ordercode,orderscore,scoreprice,scoretype from DB_Order where orderid=" + this.GridView1.DataKeys[i].Value + "").Tables[0];
                        if ((table.Rows.Count > 0) && (table.Rows[0]["scoretype"].ToString() == "0"))
                        {
                            SQLAccess.DoSql(string.Concat(new object[] { "Update DB_Member set score1=score1+", Convert.ToInt32(table.Rows[0]["orderscore"]) - Convert.ToInt32(table.Rows[0]["scoreprice"]), ",score2=score2-", Convert.ToInt32(table.Rows[0]["scoreprice"]), ",score3=score3+", Convert.ToInt32(table.Rows[0]["orderscore"]), " where memberid=", table.Rows[0][0].ToString(), "" }));
                            SQLAccess.DoSql(string.Concat(new object[] { "insert into DB_Score (memberid,scorename,scoreprice,content,updatetime,scoretype) values (", table.Rows[0][0].ToString(), ",'订单赠积分：", table.Rows[0]["ordercode"], "','+", table.Rows[0]["orderscore"], "','赠积分：", table.Rows[0]["orderscore"], "','", DateTime.Now, "',0)" }));
                        }
                    }
                    SQLAccess.DoSql(string.Concat(new object[] { "Update DB_order set states=", base.Request.Form["flag"], ",scoretype=1 where orderid=", this.GridView1.DataKeys[i].Value, "" }));
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql("delete from DB_order where orderid=" + this.GridView1.DataKeys[i].Value + "");
                    SQLAccess.DoSql("delete from DB_car where orderid=" + this.GridView1.DataKeys[i].Value + "");
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    str = str + "," + this.GridView1.DataKeys[i].Value;
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';window.open('ordera.aspx?oid=" + str + "');</script>");
            base.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.OrderCancel);
                this.Bind();
                this.GridViewBind("");
             
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }


    }
}